package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.distributor.dto.CreditTemporaryLimitDeleteBatchDTO;
import com.xbongbong.distributor.vo.CreditTemporaryLimitDeleteBatchVO;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.FormTitlePoJo;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.CellValuePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.applist.pojo.AppListPojo;
import com.xbongbong.pro.applist.pojo.vo.ListAppVO;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.pro.enums.errorcodes.CreditManagement;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainInLinkItemDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ext.CreditFixedLimitEntityExt;
import com.xbongbong.saas.domain.entity.ext.CreditLimitEntityExt;
import com.xbongbong.saas.domain.entity.ext.CreditTemporaryLimitEntityExt;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.CreditTemporaryApplyStatusEnum;
import com.xbongbong.saas.enums.DistributorCreditStatusEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.business.CreditFixedLimitEnum;
import com.xbongbong.saas.enums.business.CreditLimitEnum;
import com.xbongbong.saas.enums.business.CreditTemporaryLimitEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.help.ImportHelper;
import com.xbongbong.saas.help.SaasSaveHelp;
import com.xbongbong.saas.help.SaasUpdateHelp;
import com.xbongbong.saas.model.CreditTemporaryLimitModel;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.service.CreditFixedLimitService;
import com.xbongbong.saas.service.CreditLimitService;
import com.xbongbong.saas.service.CreditTemporaryLimitService;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author xingxing.xiao
 * @date 2020/12/25 11:13
 */
@Service("creditTemporaryLimitService")
public class CreditTemporaryLimitServiceImpl implements CreditTemporaryLimitService {

    private static final Logger LOG = LoggerFactory.getLogger(CreditTemporaryLimitServiceImpl.class);

    @Resource
    private SaasSaveHelp saasSaveHelp;
    @Resource
    private SaasUpdateHelp saasUpdateHelp;
    @Resource
    private CreditLimitService creditLimitService;
    @Resource
    private ImportHelper importHelper;
    @Resource
    private CustomerModel customerModel;
    @Resource
    private CreditFixedLimitService creditFixedLimitService;
    @Resource
    private CreditTemporaryLimitModel creditTemporaryLimitModel;
    @Resource
    private EsHelper esHelper;

    /**
     * 保存前saas特殊校验
     * @param validateDataDTO saas特殊校验数据入参
     * @throws XbbException
     */
    @Override
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        String corpid = validateDataDTO.getCorpid();
        Long temporaryId = validateDataDTO.getDataId();
        JSONObject data = validateDataDTO.getData();
        // 处理关联客户
        JSONObject customerObj = data.getJSONArray(CreditLimitEnum.CUSTOMER_ID.getAttr()).getJSONObject(0);
        String customerName = customerObj.getString(StringConstant.SAAS_LINK_BUSINESS_NAME);
        saasSaveHelp.formatLinkBusiness4Save(data, CreditTemporaryLimitEnum.CUSTOMER_ID.getAttr(), CreditTemporaryLimitEnum.CUSTOMER_NAME.getAttr(), CreditTemporaryLimitEnum.CUSTOMER_ID.getFieldType());
        boolean isNew = (temporaryId == null || Objects.equals(temporaryId, 0L));
        if (isNew) {
            // 排除不允许信用额度支付的经销商
            Long customerId = data.getLong(CreditTemporaryLimitEnum.CUSTOMER_ID.getAttr());
            CustomerEntityExt customerEntityExt = customerModel.getByKey(customerId, corpid);
            if (Objects.nonNull(customerEntityExt)) {
                JSONObject customerData = customerEntityExt.getData();
                String paymentCreditStatus = customerData.getString(DistributorEnum.PAYMENT_BY_CREDIT.getAttr());
                if (Objects.equals(paymentCreditStatus, DistributorCreditStatusEnum.NOT_ALLOW.getCode())) {
                    throw new XbbException(CreditManagement.API_ERROR_253009, String.format(I18nMessageUtil.getMessage(CreditManagement.API_ERROR_253009.getMsg()), customerName));
                }
            }
            data.put(CreditTemporaryLimitEnum.APPLY_STATUS.getAttr(), CreditTemporaryApplyStatusEnum.NORMAL.getCode());
            // 查询creditId
            CreditLimitEntityExt creditLimitEntityExt = creditLimitService.getCreditByCustomerId(customerId, corpid);
            Long creditId = Objects.nonNull(creditLimitEntityExt.getId()) ? creditLimitEntityExt.getId() : 0L;
            data.put(CreditTemporaryLimitEnum.CREDIT_ID.getAttr(), creditId);
            if (Objects.equals(validateDataDTO.getIsImport(), 1)) {
                // 校验固定额度
                Double oldLimit = data.getDouble(CreditFixedLimitEnum.OLD_LIMIT.getAttr());
                CreditFixedLimitEntityExt latestFixedLimit = creditFixedLimitService.getLatestFixedLimit(corpid, customerId);
                JSONObject fixedLimitData = latestFixedLimit.getData();
                Double applyLimit = Objects.nonNull(fixedLimitData) ? fixedLimitData.getDouble(CreditFixedLimitEnum.APPLY_LIMIT.getAttr()) : 0D;
                if (!Objects.equals(applyLimit, oldLimit)) {
                    throw new XbbException(CreditManagement.API_ERROR_253010, String.format(I18nMessageUtil.getMessage(CreditManagement.API_ERROR_253010.getMsg()), CreditTemporaryLimitEnum.FIXED_LIMIT.getAttrName()));
                }
                data.put(CreditTemporaryLimitEnum.APPLY_ID.getAttr(), validateDataDTO.getUserId());
                data.put(CreditTemporaryLimitEnum.APPLY_NAME.getAttr(), validateDataDTO.getLoginUserName());
                data.put(CreditTemporaryLimitEnum.APPLY_TIME.getAttr(), DateTimeUtil.getTodayInt());
            } else {
                if (Objects.isNull(data.get(CreditTemporaryLimitEnum.APPLY_IS_DISTRIBUTOR.getAttr()))) {
                    data.put(CreditTemporaryLimitEnum.APPLY_IS_DISTRIBUTOR.getAttr(), 0);
                    data.put(CreditTemporaryLimitEnum.APPLY_ID.getAttr(), validateDataDTO.getUserId());
                    data.put(CreditTemporaryLimitEnum.APPLY_NAME.getAttr(), validateDataDTO.getLoginUserName());
                }
            }
        }
        // 校验开始结束时间
        Long startTime = data.getLong(CreditTemporaryLimitEnum.START_TIME.getAttr());
        Long endTime = data.getLong(CreditTemporaryLimitEnum.END_TIME.getAttr());
        //在审批中的临时额度申请记录不走这层校验
        if (!Objects.nonNull(validateDataDTO.getTaskId()) && startTime.compareTo(DateTimeUtil.getTodayInt()) < 0) {
            throw new XbbException(CreditManagement.API_ERROR_253007, String.format(I18nMessageUtil.getMessage(CreditManagement.API_ERROR_253007.getMsg()), CreditTemporaryLimitEnum.START_TIME.getAttrName()));
        }
        if (endTime.compareTo(startTime) < 0) {
            throw new XbbException(CreditManagement.API_ERROR_253003);
        }
        // 额度不能为负数
        Double temporaryLimit = data.getDouble(CreditTemporaryLimitEnum.APPLY_TEMPORARY_LIMIT.getAttr());
        if (temporaryLimit < 0) {
            throw new XbbException(CreditManagement.API_ERROR_253008, String.format(I18nMessageUtil.getMessage(CreditManagement.API_ERROR_253008.getMsg()), CreditTemporaryLimitEnum.APPLY_TEMPORARY_LIMIT.getAttrName()));
        }
        Double fixedLimit = data.getDouble(CreditTemporaryLimitEnum.FIXED_LIMIT.getAttr());
        if (fixedLimit < 0) {
            throw new XbbException(CreditManagement.API_ERROR_253008, String.format(I18nMessageUtil.getMessage(CreditManagement.API_ERROR_253008.getMsg()), CreditTemporaryLimitEnum.FIXED_LIMIT.getAttrName()));
        }

    }

    /**
     * 临时额度没有删除和批量删除，目前只处理审批删除
     * @param temporaryLimitDeleteBatchDTO
     * @return
     * @throws XbbException
     */
    @Override
    public CreditTemporaryLimitDeleteBatchVO deleteBatch(CreditTemporaryLimitDeleteBatchDTO temporaryLimitDeleteBatchDTO) throws XbbException {
        CreditTemporaryLimitDeleteBatchVO temporaryLimitDeleteBatchVO = new CreditTemporaryLimitDeleteBatchVO();
        String corpid = temporaryLimitDeleteBatchDTO.getCorpid();
        Integer fromProcess = temporaryLimitDeleteBatchDTO.getFromProcess();
        try {
            // 查询所选列表
            List<Long> dataIdIn = temporaryLimitDeleteBatchDTO.getDataIdList();
            Map<String, Object> param = BeanUtil.convertBean2Map(temporaryLimitDeleteBatchDTO, true);
            param.put("idIn", temporaryLimitDeleteBatchDTO.getDataIdList());
            List<CreditTemporaryLimitEntityExt> temporaryLimitList = creditTemporaryLimitModel.findEntitys(param);
            if (Objects.isNull(temporaryLimitList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            if (!dataIdIn.isEmpty()) {
                if (BasicConstant.ONE.equals(temporaryLimitDeleteBatchDTO.getSingleFlag())
                        && BasicConstant.ONE.equals(dataIdIn.size())) {
                    if (Objects.equals(fromProcess, 1)) {
                        // 更改成作废的del状态
                        creditTemporaryLimitModel.deleteByKey4Process(dataIdIn.get(0), corpid);
                    }
                }
            }
        } catch (XbbException e){
            throw e;
        } catch (Exception e) {
            LOG.error("creditTemporaryLimitServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return temporaryLimitDeleteBatchVO;
    }

    /**
     * 名称一致性同步
     * @param corpid
     * @param customerId
     * @param customerName
     * @return
     * @author xingxing.xiao
     * @date 2021/02/25 16:48
     */
    @Override
    public void dataConsistencyUpdateCustomer(String corpid, Long customerId, String customerName) throws XbbException {
        try {
            BoolQueryBuilder filter = boolQuery()
                    .filter(termQuery("corpid.keyword", corpid))
                    .filter(termQuery("del", 0))
                    .filter(termQuery(CreditTemporaryLimitEnum.getEsAttr4Keyword(CreditTemporaryLimitEnum.CUSTOMER_ID), customerId));
            List<String> fieldList = new ArrayList<>();
            fieldList.add(FieldTypeEnum.DATAID.getAlias());
            List<PaasFormDataEntityExt> esEntities = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_CREDIT_TEMPORARY_LIMIT, filter, PaasFormDataEntityExt.class, fieldList);
            Set<Long> ids = new HashSet<>();
            for (PaasFormDataEntityExt esEntity : esEntities) {
                ids.add(esEntity.getDataId());
            }
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("idIn", ids);
            param.put("del", 0);
            List<CreditTemporaryLimitEntityExt> entityList = creditTemporaryLimitModel.findEntitys(param);
            if (CollectionUtils.isNotEmpty(entityList)) {
                List<UpdateDataEntity> updateList = new ArrayList<>();
                for (CreditTemporaryLimitEntityExt entity : entityList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put(CreditTemporaryLimitEnum.CUSTOMER_NAME.getAttr(), customerName);
                    UpdateDataEntity updateData = ExplainUtil.getUpdateData(entity.getId(), jsonObject, corpid);
                    updateList.add(updateData);
                }
                creditTemporaryLimitModel.updateBatch(updateList, corpid);
            }
        } catch (Exception e) {
            LOG.error("creditTemporaryLimitServiceImpl.dataConsistencyUpdateCustomer 出错， corpid=" + corpid + " customerId=" + customerId + " customerName=" + customerName, e);
        }
    }

    /**
     * 保存后逻辑
     * @param saasFormSaveDTO saas保存数据
     * @throws XbbException
     */
    @Override
    public void afterSave(SaasFormSaveDTO saasFormSaveDTO) throws XbbException {
        /*PaasFormDataEntity newPaasFormDataEntity = saasFormSaveDTO.getNewPaasFormDataEntity();
        Boolean isNew = saasFormSaveDTO.getIsNew();
        if (isNew) {
            StartProcessDTO startProcessDTO = new StartProcessDTO();
            BeanUtil.copyProperties(saasFormSaveDTO, startProcessDTO);
            BeanUtil.copyProperties(newPaasFormDataEntity, startProcessDTO);
            startProcessDTO.setFormDataId(newPaasFormDataEntity.getId());
            startProcessDTO.setDistributorMark(DistributorMarkEnum.DISTRIBUTOR.getCode());
            startProcessDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
            StartProcessVO startProcessVO = postPositionProcessHelp.startProcess(startProcessDTO);
            if (Objects.equals(startProcessVO.getNoApprovalRequired(), 0)) {
                CreditTemporaryLimitEntityExt creditTemporaryLimitEntityExt = new CreditTemporaryLimitEntityExt();
                creditTemporaryLimitEntityExt.setCorpid(saasFormSaveDTO.getCorpid());
                creditTemporaryLimitEntityExt.setId(startProcessDTO.getFormDataId());
                creditTemporaryLimitEntityExt.setFlowStatus(FlowStatusEnum.IN_APPROVAL.getType());
                creditTemporaryLimitModel.update(creditTemporaryLimitEntityExt);
            }
        }*/


    }

    /**
     * 编辑时将获取数据格式化
     *
     * @param saasAnalysisDataDTO saas编辑数据
     * @param result 要返回的jsonobject
     * @throws XbbException 业务异常
     */
    @Override
    public void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException {
        creditTemporaryLimitModel.formatData(saasAnalysisDataDTO, result);
    }

    /**
     * 关联新建时格式化解释
     *
     * @param handlerExplainInLinkItemDTO 入参
     * @throws XbbException 异常
     */
    @Override
    public void formatLinkAddExplain(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException {
        JSONObject linkFormData = handlerExplainInLinkItemDTO.getLinkFormData();
        // Long linkDataId = handlerExplainInLinkItemDTO.getLinkDataId(); 此处放的是 customerId, 不使用 linkDataId
        String customerIdStr = linkFormData.getString(CreditTemporaryLimitEnum.CUSTOMER_ID.getAttr());
        Long customerId = Objects.nonNull(customerIdStr) ? Long.parseLong(customerIdStr) : 0L;
        List<FieldAttrEntity> explainList = handlerExplainInLinkItemDTO.getExplainList();
        FieldAttrEntity linkCustomerField = new FieldAttrEntity();
        for(FieldAttrEntity fieldAttrEntity :explainList) {
            if (Objects.equals(fieldAttrEntity.getAttr(), CreditTemporaryLimitEnum.CUSTOMER_ID.getAttr())) {
                linkCustomerField = fieldAttrEntity;
            }
        }
        RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(handlerExplainInLinkItemDTO.getLinkBusinessType());
        switch (redundantTemplateTypeEnum) {
            case CREDIT_LIMIT:
                String customerName = FastJsonHelper.getStringOrDefaultFromFormData(linkFormData, CreditLimitEnum.CUSTOMER_NAME.getAttr(), "");
                linkCustomerField.setDefaultAttr(saasUpdateHelp.getDefaultAttrPojo(customerId, customerName));
                break;
            default:
                break;
        }
    }

    /**
     * 新建时格式化解释
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     */
    @Override
    public void formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        explainList.forEach(item -> {
            if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.APPLY_NAME.getAttr())) {
                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                defaultAttrPoJo.setDefaultType(StringConstant.CUSTOM);
                defaultAttrPoJo.setDefaultValue(formatExplainDTO.getLoginUser().getName());
                item.setDefaultAttr(defaultAttrPoJo);
                // 为了在页面展示，需去除unableEditMemo
                item.setUnableEditMemo(null);
                item.setVisible(1);
                item.setEditable(0);
            } else if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.START_TIME.getAttr())) {
                SaasParticularAttributePoJo saasParticularAttributePoJo = item.getSaasParticularAttributePoJo();
                saasParticularAttributePoJo = saasParticularAttributePoJo == null ? new SaasParticularAttributePoJo() : saasParticularAttributePoJo;
                saasParticularAttributePoJo.setDateLimit(DateTimeUtil.getTodayInt());
                saasParticularAttributePoJo.setLimitType(false);
                item.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
            } else if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.END_TIME.getAttr())) {
                DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
                defaultAttrPoJo.setDefaultType(StringConstant.CUSTOM);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, 30);
                defaultAttrPoJo.setDefaultValue(calendar.getTimeInMillis() / 1000);
                item.setDefaultAttr(defaultAttrPoJo);
            } else if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.APPLY_TIME.getAttr())) {
                item.setEditable(0);
            } else if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.FIXED_LIMIT.getAttr())) {
                item.setEditable(0);
            } else if (Objects.equals(item.getAttr(), CreditTemporaryLimitEnum.CREDIT_TEMPORARY_NO.getAttr())) {
                item.setEditable(0);
            }
        });
    }

    /**
     * 格式化导出前置处理
     *
     * @param paasFormDataESList 数据
     * @throws XbbException 异常
     */
    @Override
    public void formatExportValueBefore(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO) throws XbbException {
        for (PaasFormDataEntityExt entityExt : paasFormDataESList) {
            JSONObject data = entityExt.getData();
            // 过期状态处理
            String temporaryStatus = data.getString(CreditTemporaryLimitEnum.APPLY_STATUS.getAttr());
            int endTime = data.getIntValue(CreditTemporaryLimitEnum.END_TIME.getAttr());
            if (endTime < DateTimeUtil.getTodayInt() && !Objects.equals(temporaryStatus, CreditTemporaryApplyStatusEnum.REVOKED.getCode())) {
                data.put(CreditTemporaryLimitEnum.APPLY_STATUS.getAttr(), CreditTemporaryApplyStatusEnum.EXPIRED.getCode());
            }
        }
    }

    /**
     * 格式化导出值
     *
     * @param paasFormDataESList 数据
     * @throws XbbException 异常
     */
    @Override
    public void formatExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException {

    }

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO 导入入参
     * @param titlePojoList     标题
     * @param rowGroupPojo      一个表单对应的一组Excel填写的数据
     * @param fieldAttrList     解释
     * @return FormDataAddDTO
     */
    @Override
    public FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException {
        FormDataAddDTO formDataAddDTO = new FormDataAddDTO();
        BeanUtil.copyProperties(importFormDataDTO, formDataAddDTO);
        formDataAddDTO.setSerialNo("");
        JSONObject dataJson = new JSONObject();
        List<List<CellValuePojo>> rowValueList = rowGroupPojo.getRowValueList();
        // 分组的第一条数据作为给实体对象赋值的数据
        List<CellValuePojo> cellValueList = rowValueList.get(0);
        for (FieldAttrEntityForImport fieldAttr : fieldAttrList) {
            String attr = fieldAttr.getAttr();
            Integer fieldType = fieldAttr.getFieldType();
            String attrName = fieldAttr.getAttrName();
            Integer columnIndex = importHelper.getColumnIndexByTitle(titlePojoList, attrName, null);
            Object cellValue = importHelper.getValueByColumnIndex(cellValueList, columnIndex);
            if (Objects.equals(fieldType, FieldTypeEnum.SUB_FORM.getType())) {
                importHelper.formatSubFormValue4Import(rowValueList, titlePojoList, importFormDataDTO, fieldAttr, dataJson);
            } else {
                if (Objects.isNull(cellValue) || StringUtil.isEmpty(cellValue.toString())) {
                    continue;
                }
                if (ImportHelper.commonHandleFieldType.contains(fieldAttr.getFieldType())) {
                    importHelper.formatCommonField(dataJson, fieldAttr, cellValue);
                } else if (Objects.equals(fieldType, FieldTypeEnum.SERIALNO.getType())) {
                    formDataAddDTO.setSerialNo(cellValue.toString());
                } else if (Objects.equals(fieldType, FieldTypeEnum.LINK_DATA.getType())) {
                    importHelper.formatLinkData(cellValue, fieldAttr, dataJson);
                } else if (Objects.equals(fieldType, FieldTypeEnum.LINK_DATA_MULTI.getType())) {
                    importHelper.formatLinkDataMulti(cellValue, importFormDataDTO, fieldAttr, dataJson);
                } else {
                    dataJson.put(attr, cellValue);
                }
            }
        }
        formDataAddDTO.setDataList(dataJson);
        return formDataAddDTO;
    }

    /**
     * 格式化移动端列表页
     * @param formDataListDTO 用户
     * @param esEntities 业务实体集合
     * @param totalCount 总数
     * @return
     * @throws XbbException
     */
    @Override
    public ListAppVO formatCreditTemporaryAppList(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt> esEntities, Integer totalCount) throws XbbException {
        List<AppListPojo> appLists = new ArrayList<>();
        for (PaasFormDataEntityExt entity : esEntities) {
            JSONObject data = entity.getData();
            JSONObject superScript = new JSONObject();
            JSONObject others = new JSONObject();
            List<SummaryDataPoJo> summaryList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.SUMMARY_LIST, new JSONArray()).toJavaList(SummaryDataPoJo.class);
            List<SummaryDataPoJo> lablesList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.LABELS_LIST, new JSONArray()).toJavaList(SummaryDataPoJo.class);
            List<FormTitlePoJo> titleList = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, BasicConstant.TITLE_LIST, new JSONArray()).toJavaList(FormTitlePoJo.class);
            appLists.add(new AppListPojo(entity.getId(), titleList, summaryList, superScript, lablesList, others, entity.getAppId(), entity.getMenuId(), entity.getFormId()));
        }

        ListAppVO listAppVO = new ListAppVO();
        listAppVO.setList(appLists);
        listAppVO.setTotalCount(totalCount);
        return  listAppVO;
    }
}
